package com.zimi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zimi.file.mapper.SysFileMapper;
import com.zimi.file.model.SysFile;
import com.zimi.mapper.CompanyBusinessLicenceMapper;
import com.zimi.model.CompanyBusinessLicence;
import com.zimi.model.SysUser;
import com.zimi.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * 营业执照信息 Service
 *
 * @author lnintr
 * @since 2019-07-04
 */
@Service
public class CompanyBusinessLicenceService {
    @Autowired
    private CompanyBusinessLicenceMapper companyBusinessLicenceMapper;

    @Autowired
    private SysFileMapper fileMapper;

    @Value("${zimi.profile:'file'}")
    private String basePath;//文件上传路径

    @Transactional
    public String add(CompanyBusinessLicence companyBusinessLicence) {
        SysUser user = UserUtil.getUser();
        Date updateTime = new Date();

        //若数据库上存在相同的信用代码
        CompanyBusinessLicence result = companyBusinessLicence.selectOne(new QueryWrapper<CompanyBusinessLicence>().lambda().eq(CompanyBusinessLicence::getCreditCode, companyBusinessLicence.getCreditCode()));
        if (result != null) {
            //若上传数据的成立时间比数据库的新
            if (companyBusinessLicence.getEstablishmentDate().getTime() > result.getEstablishmentDate().getTime()) {
                //删除数据库数据的附件
                try {
                    deleteFile(result.getFileId());
                } catch (IOException e) {
                    e.printStackTrace();
                    return "您上传的文件注册时服务器发生异常！";
                }

                companyBusinessLicence.setUpdateUser(user.getId());
                companyBusinessLicence.setUpdateTime(updateTime);
                companyBusinessLicence.setGmtState(1);
                companyBusinessLicence.setId(result.getId());
                companyBusinessLicence.updateById();

            } else {
                //删除上传数据的附件
                try {
                    deleteFile(companyBusinessLicence.getFileId());
                } catch (IOException e) {
                    e.printStackTrace();
                    return "您上传的文件注册时服务器发生异常！";
                }

                return "您上传的文件已注册过！";
            }
        } else {
            companyBusinessLicence.setCreateTime(new Date());
            companyBusinessLicence.setGmtState(1);
            companyBusinessLicence.setCreateUser(user.getId());
            companyBusinessLicence.insert();
        }

        return "";
    }

    private void deleteFile(int fileId) throws IOException {
        //数据冗余 删除文件
        SysFile fileInfo = fileMapper.selectByPrimaryKey(fileId);
        String fileName = fileInfo.getFileName();
        String filePath = fileInfo.getFilePath();
        String folder = filePath.substring(filePath.lastIndexOf("\\"));

        //删除数据库数据
        fileMapper.deleteByPrimaryKey(fileId);
        //删除物理文件
        Path path = Paths.get(basePath, folder, fileName);
        Files.deleteIfExists(path);
    }

}
